package com.eee.machine.service.impl;

import com.eee.machine.beans.TChannelProduct;
import com.eee.machine.common.OperateResponse;
import com.eee.machine.common.TableData;
import com.eee.machine.configuration.StoreConfig;
import com.eee.machine.dao.mapper.TChannelProductMapper;
import com.eee.machine.service.IProductService;
import com.eee.machine.utils.youzan.TokenUtil;
import com.youzan.open.sdk.client.auth.Token;
import com.youzan.open.sdk.client.core.DefaultYZClient;
import com.youzan.open.sdk.client.core.YZClient;
import com.youzan.open.sdk.client.oauth.model.OAuthToken;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanItemsOnsaleGet;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanRetailOpenSpuQuery;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemsOnsaleGetParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemsOnsaleGetResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanRetailOpenSpuQueryParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanRetailOpenSpuQueryResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private StoreConfig storeConfig;
    @Autowired
    private TChannelProductMapper channelProductMapper;

    @Override
    public TableData getAllProduct(Long page, Long pageSize) {
//        System.out.println("授权码"+storeConfig.getCode());
//        OAuthToken token = TokenUtil.getToken();
//        YZClient client = new DefaultYZClient(new Token(token.getAccessToken())); //new Sign(appKey, appSecret)
        String accessToken = tokenUtil.getAccessToken();
        System.out.println("授权token====="+accessToken);
        YZClient client = new DefaultYZClient(new Token(accessToken)); //new Sign(appKey, appSecret)

        YouzanItemsOnsaleGetParams youzanItemsOnsaleGetParams = new YouzanItemsOnsaleGetParams();
        youzanItemsOnsaleGetParams.setPageNo(page);
        youzanItemsOnsaleGetParams.setPageSize(pageSize);

        YouzanItemsOnsaleGet youzanItemsOnsaleGet = new YouzanItemsOnsaleGet();
        youzanItemsOnsaleGet.setAPIParams(youzanItemsOnsaleGetParams);

        YouzanItemsOnsaleGetResult result = client.invoke(youzanItemsOnsaleGet);

//        YouzanRetailOpenSpuQueryParams youzanRetailOpenSpuQueryParams = new YouzanRetailOpenSpuQueryParams();
////        youzanRetailOpenSpuQueryParams.setCreateTimeStart("2017-01-13 00:00:00");
//        youzanRetailOpenSpuQueryParams.setPageSize(pageSize);
//        youzanRetailOpenSpuQueryParams.setPageNo(page);
//        youzanRetailOpenSpuQueryParams.setRetailSource(storeConfig.getRetailSource());
////        youzanRetailOpenSpuQueryParams.setSpuCodes("["BM45086697881"]");
//        YouzanRetailOpenSpuQuery youzanRetailOpenSpuQuery = new YouzanRetailOpenSpuQuery();
//        youzanRetailOpenSpuQuery.setAPIParams(youzanRetailOpenSpuQueryParams);
//        YouzanRetailOpenSpuQueryResult result = client.invoke(youzanRetailOpenSpuQuery);

        TableData resultObject = new TableData("获取数据成功", 0 , result.getCount().intValue(), result.getItems());
        return resultObject;
    }

    @Override
    public OperateResponse checkGood(String itemId, String machineId) {
        TChannelProduct tChannelProduct = channelProductMapper.checkGood(itemId, machineId);
        return OperateResponse.obtainSuccessResponse(tChannelProduct);
    }
}
